// SlagCar/DailyCheckNew/DailyCheckNew.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //出车标记，登记未出车时按钮禁用
    btnDisabled: false,

    entity: 'e_cheliangsanjian',

  //  检查状态
    checkStatus: ['','','']
  },
  //修改出车状态
  changeStatus(){
    let {row_id} = this.data,
      content = row_id ? '今日已出车是否作废之前的检查，重新登记为未出车？' : '确定修改车辆今日状态为未出车？';
    wx.showModal({
      title: '提示',
      content,
      confirmText: '确定',
      success: (res) => {
        if(res.confirm) {
          this.submitStatus();
        }
      },
    });
  },
  //提交出车状态
  submitStatus(){
    let {row_id} = this.data,
      // post = {is_departed: false,is_generate: !row_id},
      post = {is_departed: false,is_generate: true},
      url = `ZTAuth/CheLiangSanJian`;
    wx.showLoading({mask: true});
    wx.$post(5,url,post,).then(res =>{
      if (res.errcode != 0 )return;
      wx.$toast('修改成功');
      this.getStatus();
    })
  },
  //获取今日出车状态
  async getStatus(){
    wx.showLoading({mask: true})
    let {entity} = this.data,
      record;
    await this.getCheckRecord().then(res => record = res)
    if (record) {
      let row_id = record?.row_id;
      entity = record?.table_id;
      let post = {
        "oby": `${entity}.row_id`,
        "table_id": entity,
        "page":1,
        "limit":30,
        "SearchWhere":{
          "filters":[
            {"CompareWith": `${entity}.row_id`,"Operator":"=","CompareValue": row_id,"or":0},
            // {"CompareWith": `${entity}.is_departed`,"Operator":"=","CompareValue": false,"or":0}
          ],
          "type": 'and'
        },
      };

      this.getCustomData(post).then(res =>{
        wx.hideLoading();
        let data = res[0],
          checkStatus = [],
          fieldArr = ['chucheqianjianchashijian','xingchetuzhongjianchashijian','shouchehoujianchashijian']
        if (data){
          fieldArr.forEach((item,index) =>{
            checkStatus[index] = data[item]
          })
          this.setData({btnDisabled: !data.is_departed,checkStatus,row_id})
        }
      });
      return
    }
    wx.hideLoading();
    this.setData({row_id: null})

  },

  //提交作废接口
  submitCancel(){
    return new Promise(resolve => {
      let url = 'ZTAuth/CheLiangSanJian',data = {is_cancel: true};
      wx.$post(5,url,data).then(res =>{
        if (res.errcode != 0) return;
        resolve()
      })
    })
  },

  /**
   * 获取车辆三检记录行号
   * @param car {String} 车牌号码
   * @return {Promise<any>}
   */
  getCheckRecord(car){
    return new Promise(resolve => {
      let url = 'ZTAuth/CheLiangSanJian';
      wx.$get(5,url).then(res =>{
        if (res.errcode != 0 )return;
        let result = res.data.row?.[0];
        resolve(result)

      })
    })
  },

  //获取自定义模块数据
  getCustomData(post){
    return new Promise(resolve => {
      let url = `DynamicQuery/query_dynamic_v3`
      wx.$post(3,url,post).then(res =>{
        if (res.errcode != 0) return;
        let data = res.data.row || [];
        resolve(data)

      })
    })
  },

  //开始新的检查
  startNewCheck(){
    wx.showModal({
      title: '提示',
      content: '确认是否开始新的检查？',
      confirmText: '确定',
      success: (res) => {
        if(res.confirm) {
          wx.navigateTo({url: '/SlagCar/DailyCheckDetl/DailyCheckDetl'})
        }
      },
    });
  },
  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({url})
  },
  //检查
  bindCheck(e){
    let {url,type} = e.currentTarget.dataset,
      {btnDisabled} = this.data;
    if (btnDisabled){
      wx.showModal({
        title: '提示',
        content: '今日已标记未出车，是否作废标记重新开始检查？',
        success: (res) =>{
          if (res.confirm){
            this.submitCancel().then(() => wx.navigateTo({url}))
          }
        }
      })
    } else {
      wx.navigateTo({url})
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let driver_car_row_id = wx.getStorageSync('driver_car_row_id'),
      driver_car_name = wx.getStorageSync('driver_car_name');
    this.setData({driver_car_name,driver_car_row_id})

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStatus()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
